######################
#Replication Materials
######################

################
#Poisson Models 
#################
rm(list = ls())
#setwd("~/Dropbox (Business)/Smolensk Conspiracy Blackington Cayton/Smolensk Conspiracy Blackington Cayton/POP Final Submission/Replication POP Requirements")
setwd("~/Dropbox/Smolensk Conspiracy Blackington Cayton/Smolensk Conspiracy Blackington Cayton/POP Final Submission/Replication POP Requirements")

# Packages
pacman::p_load(rtweet,
               ggplot2,
               purrrlyr,
               dplyr,
               readxl,
               writexl,
               broom,
               tidyverse,
               dotwhisker,
               lubridate,
               stargazer,
               ggpubr, 
               jtools,
               rpart,
               caret,
               reshape2,
               ipred,
               RColorBrewer,
               rpart.plot,
               MASS,
               randomForest,
               dplyr,
               car,
               vtable,
               effects,
               interactions,
               doParallel,
               foreach
               
)

set.seed(0611)
#Import data
df <- readRDS("df.RDS")


#######
#######
# Models -- Basic
#######
######
#Likes
m_1 <- glm(favorite_count ~ conspir.po + log_follow + 
            log_friends + verified + month, 
          family = "poisson",
          data = df)
m_2 <- glm(favorite_count ~ conspir.tusk + log_follow + 
            log_friends + verified + month, 
          family = "poisson",
          data = df)


m_3 <- glm(favorite_count ~ conspir.collusion + log_follow + 
            log_friends + verified + month, 
          family = "poisson",
          data = df)

stargazer(m_1, m_2, m_3, 
          header = FALSE,
          align = TRUE,
          type = "text",
          column.sep.width = "-15pt",
          title = "Tweet Characteristics and Frequency of Likes-Poisson Models",
          dep.var.caption = c("Number of Likes"),
          model.names = FALSE,
          omit = c("month"), 
          covariate.labels = c("PO Consp.",
                               "Tusk Consp.",
                               
                               "Russia and PO Consp.",
                               
                               "Log(N Followers)",
                               "Log(N Friends)",
                               "Verified",
                               "Constant"),
          notes.append = FALSE,
          notes = c("Standard Errors in parenthesis.", 
                    "*p $<$ 0.1; **p $<$ .05, ***p $<$ .01 (two-tailed tests)."))

#Retweets
m_5 <- glm(retweet_count ~ conspir.po + log_follow + 
            log_friends + verified + month, 
          family = "poisson",
          data = df)

m_6 <- glm(retweet_count ~ conspir.tusk + log_follow + 
            log_friends + verified + month, 
          family = "poisson",
          data = df)

m_7 <- glm(retweet_count ~ conspir.collusion + log_follow + 
            log_friends + verified + month, 
          family = "poisson",
          data = df)

stargazer(m_5, m_6, m_7, 
          header = FALSE,
          align = TRUE,
          type = "text",
          #column.sep.width = "-15pt",
          dep.var.caption = c("Number of Retweets"),
          model.names = FALSE,
          omit = c("month"), 
          covariate.labels = c("PO Consp.",
                               "Tusk Consp.",
                               "Russia Consp.",
                               "Russia and PO Consp.",
                               
                               "Log(N Followers)",
                               "Log(N Friends)",
                               "Verified",
                               "Constant"),
          notes.append = FALSE,
          notes = c("Standard Errors in parenthesis.", 
                    "*p $<$ 0.1; **p $<$ .05, ***p $<$ .01 (two-tailed tests)."))

#########
########
#Models -- Officials
#######
######
m1o <- glm(favorite_count ~ conspir.po*officials + log_follow + 
            log_friends + verified + month, 
          family = "poisson",
          data = df)
m2o <- glm(favorite_count ~ conspir.tusk*officials + log_follow + 
            log_friends + verified + month, 
          family = "poisson",
          data = df)

m3o <- glm(favorite_count ~ conspir.collusion*officials + log_follow + 
            log_friends + verified + month, 
          family = "poisson",
          data = df)
stargazer(m1o, m2o, m3o, 
          header = FALSE,
          align = TRUE,
          type = "text",
          #out = "Figures for Paper/app_fav_conspir_quasi.htm",
          #column.sep.width = "-15pt",
          dep.var.caption = c("Number of Likes"),
          model.names = FALSE,
          omit = c("month"), #so it's prettier
          covariate.labels = c("PO Consp.",
                               "Tusk Consp.",
                               "Russia and PO Consp.",
                               "PiS Officials",
                               "Log(N Followers)",
                               "Log(N Friends)",
                               "Verified",
                               "PO Consp.*PiS Official",
                               "Tusk Consp.*PiS Official",
                               "PO and Russia Consp.*PiS Official",
                               "Constant"),
          notes.append = FALSE,
          notes = c("Standard Errors in parenthesis.", 
                    "*p $<$ 0.1; **p $<$ .05, ***p $<$ .01 (two-tailed tests)."))

#repeat for retweets
m1_o <- glm(retweet_count ~ conspir.po*officials + log_follow + 
            log_friends + verified + month, 
          family = "poisson",
          data = df)

m2_o <- glm(retweet_count ~ conspir.tusk*officials + log_follow + 
            log_friends + verified + month, 
          family = "poisson",
          data = df)


m3_o <- glm(retweet_count ~ conspir.collusion*officials + log_follow + 
            log_friends + verified + month, 
          family = "poisson",
          data = df)

stargazer(m1_o, m2_o, m3_o, 
          header = FALSE,
          align = TRUE,
          type = "text",
          #column.sep.width = "-15pt",
          dep.var.caption = c("Number of Retweets"),
          model.names = FALSE,
          omit = c("month"), #so it's prettier
          covariate.labels = c("PO Consp.",
                               "Tusk Consp.",
                               "Russia and PO Consp.",
                               "PiS Official",
                               "Log(N Followers)",
                               "Log(N Friends)",
                               "Verified",
                               "PO Consp.*PiS Official",
                               "Tusk Consp.*PiS Official",
                               "Russia and PO Consp.*PiS Official",
                               "Constant"),
          notes.append = FALSE,
          notes = c("Standard Errors in parenthesis.", 
                    "*p $<$ 0.1; **p $<$ .05, ***p $<$ .01 (two-tailed tests)."))
#########
#########
# Models -- Focusing Event
#########
#########
# Creating wartime variable 
df %>%
  mutate(War = created_at > "2022-02-24") -> df

# coercing for formatting 
df$War <- ifelse(df$War == "TRUE", 1, 0)
##########
#Likes
##########

m.conspir.po.fav <- glm(favorite_count ~ conspir.po*War + log_follow + 
                          log_friends + verified + month, 
                        family = "poisson",
                        data = df)
m.conspir.tusk.fav <- glm(favorite_count ~ conspir.tusk*War + log_follow + 
                            log_friends + verified + month, 
                          family = "poisson",
                          data = df)
m.conspir.collusion.fav <- glm(favorite_count ~ conspir.collusion*War + log_follow + 
                                 log_friends + verified + month, 
                               family = "poisson",
                               data = df)

m.conspir.po.rt <- glm(retweet_count ~ conspir.po*War + log_follow + 
                         log_friends + verified + month, 
                       family = "poisson",
                       data = df)

m.conspir.tusk.rt <- glm(retweet_count ~ conspir.tusk*War  + log_follow + 
                           log_friends + verified + month, 
                         family = "poisson",
                         data = df)

m.conspir.collusion.rt <- glm(retweet_count ~ conspir.collusion*War  + log_follow + 
                                log_friends + verified + month, 
                              family = "poisson",
                              data = df)

#Focusing Event Poisson Models
stargazer(m.conspir.po.fav, m.conspir.tusk.fav, m.conspir.collusion.fav,
          header = FALSE,
          align = TRUE,
          type = "text",
          column.sep.width = "5pt",
          title = "Tweet Characteristics and Frequency of Likes-Poisson Models",
          dep.var.caption = c("Number of Likes"),
          model.names = FALSE,
          omit = c("month"), #so it's prettier
          covariate.labels = c("PO Consp.",
                               "Tusk Consp.",
                               "Russia and PO Consp.",
                               "War",
                               "Log(Followers)",
                               "Log(Friends)",
                               "Verified",
                               "PO Consp.*War",
                               "Tusk Consp.*War",
                               "Russia and PO Consp.*War",
                               "Constant"),
          notes.append = FALSE,
          notes = c("Standard Errors in parenthesis.", 
                    "*p $<$ 0.1; **p $<$ .05, ***p $<$ .01 (two-tailed tests)."))


stargazer(m.conspir.po.rt, m.conspir.tusk.rt, m.conspir.collusion.rt, 
          header = FALSE,
          align = TRUE,
          type = "text",
          label = "tab:war_app_rt_conspir_poisson",
          #column.sep.width = "-15pt",
          title = "Tweet Characteristics and Frequency of Retweets-Poisson Models",
          dep.var.caption = c("Number of Retweets"),
          model.names = FALSE,
          omit = c("month"), #so it's prettier
          covariate.labels = c("PO Consp.",
                               "Tusk Consp.",
                               "Russia and PO Consp.",
                               "War",
                               "Log(N Followers)",
                               "Log(N Friends)",
                               "Verified",
                               "PO Consp.*War",
                               "Tusk Consp.*War",
                               "Russia & PO Consp.*War",
                               "Constant"),
          notes.append = FALSE,
          notes = c("Standard Errors in parenthesis.", 
                    "*p $<$ 0.1; **p $<$ .05, ***p $<$ .01 (two-tailed tests)."))


#########
#########
# Models -- Triple Interactions
#########
#########

##########
#Likes
##########
m1 <- glm(favorite_count ~ conspir.po*War*officials + log_follow + 
            log_friends + verified + month, 
          family = "poisson",
          data = df)

m2 <- glm(favorite_count ~ conspir.tusk*War*officials + log_follow + 
            log_friends + verified + month, 
          family = "poisson",
          data = df)

m3 <- glm(favorite_count ~ conspir.collusion*War*officials + log_follow + 
            log_friends + verified + month, 
          family = "poisson",
          data = df)
stargazer(m1, m2, m3,
          type = "text",
          header = FALSE,
          align = TRUE,
          label = "tab:triple.inter.qp",
          title = "Tweet Characteristics and Frequency of Likes- Poisson Models",
          dep.var.labels = c("Number of Likes"
                             # "Number of Retweets"
          ),
          model.names = FALSE,
          omit = c("month"), #so it's prettier
          covariate.labels = c("PO Consp.",
                               "Tusk Consp.",
                               "Russia and PO Consp.",
                               "War",
                               "PiS Officials",
                               "Log(N Followers)",
                               "Log(N Friends)",
                               "Verified",
                               "PO Consp.*War",
                               "PO Consp.*PiS Official",
                               "Tusk Consp.*War",
                               "Tusk Consp.*PiS Official",
                               "Collusion Consp.*War",
                               "Collusion Consp.*PiS Official",
                               "War*PiS Official",
                               "PO Consp.*War*Official",
                               "Tusk Consp.*War*Official",
                               "Collusion Consp.*War*Official",
                               "Constant"),
          notes.append = FALSE,
          notes = c("Standard Errors in parenthesis.", 
                    "*p $<$ 0.1; **p $<$ .05, ***p $<$ .01 (two-tailed tests)."))
##########
#Retweets
##########

m4 <- glm(retweet_count ~ conspir.po*War*officials + log_follow + 
            log_friends + verified + month, 
          family = "poisson",
          data = df)
m5 <- glm(retweet_count ~ conspir.tusk*War*officials + log_follow + 
            log_friends + verified + month, 
          family = "poisson",
          data = df)

m6 <- glm(retweet_count ~ conspir.collusion*War*officials + log_follow + 
            log_friends + verified + month, 
          family = "poisson",
          data = df)
stargazer(m4, m5, m6, 
          type = "text",
          header = FALSE,
          align = TRUE,
          
          dep.var.labels = c(#"Number of Likes",
            "Number of Retweets"
          ),
          model.names = FALSE,
          omit = c("month"), #so it's prettier
          covariate.labels = c("PO Consp.",
                               "Tusk Consp.",
                               "Russia and PO Consp.",
                               
                               "War",
                               "PiS Officials",
                               "Log(N Followers)",
                               "Log(N Friends)",
                               "Verified",
                               "PO Consp.*War",
                               "PO Consp.*PiS Official",
                               "Tusk Consp.*War",
                               "Tusk Consp.*PiS Official",
                               "Collusion Consp.*War",
                               "Collusion Consp.*PiS Official",
                               
                               "War*PiS Official",
                               "PO Consp.*War*Official",
                               "Tusk Consp.*War*Official",
                               "Collusion Consp.*War*Official",
                               
                               "Constant"),
          notes.append = FALSE,
          notes = c("Standard Errors in parenthesis.", 
                    "*p $<$ 0.1; **p $<$ .05, ***p $<$ .01 (two-tailed tests)."))
